if myHero.charName ~= "Ezreal" and VIP_USER then return end

require "VPrediction"
require "Collision"

function menu()
--Menu Start
	ezMenu = scriptConfig("Ezreal MMA Standalone", "Ezreal")
--Sub Menus
	ezMenu:addSubMenu(scriptName .. " - MMA Settings", "key")
	ezMenu:addSubMenu(scriptName .. " - VPrediction Settings", "vPred")
	ezMenu:addSubMenu(scriptName .. " - Farm Settings", "farm")
	ezMenu:addSubMenu(scriptName .. " - Draw Settings", "draw")
--General Settings
	ezMenu.key:addParam("comboQ","Use Q" ,SCRIPT_PARAM_ONOFF, true)
	ezMenu.key:addParam("comboW","Use W" ,SCRIPT_PARAM_ONOFF, true)
	ezMenu.key:addParam("comboR","Use R" ,SCRIPT_PARAM_ONOFF, true)
--Q Farm Settings
	ezMenu.farm:addParam("useQ","Use Q",SCRIPT_PARAM_ONOFF, false)
	ezMenu.farm:addParam("farmQMana", "Min Mana to Q", SCRIPT_PARAM_SLICE, 30, 0, 100, 0)
--VPrediction Settings
	ezMenu.vPred:addParam("qHitChance","HitChance - Mystic Shot", SCRIPT_PARAM_SLICE, 2, 0, 5, 0)
	ezMenu.vPred:addParam("qChange", "Range - Mystic Shot", SCRIPT_PARAM_SLICE, 1100, 550, 1100, 0)
	ezMenu.vPred:addParam("wHitChance","HitChance - Essence Flux", SCRIPT_PARAM_SLICE, 2, 0, 5, 0)
	ezMenu.vPred:addParam("wChange", "Range - Essence Flux", SCRIPT_PARAM_SLICE, 1050, 550, 1050, 0)
	ezMenu.vPred:addParam("rHitChance","HitChance - True Shot Barrage", SCRIPT_PARAM_SLICE, 2, 0, 5, 0)
	ezMenu.vPred:addParam("rChange", "Range - True Shot Barrage", SCRIPT_PARAM_SLICE,2000, 100, 2000, 0)
--Draw Settings
	ezMenu.draw:addParam("allDraw", "Disable All Ranges Drawing", SCRIPT_PARAM_ONOFF, false)
	ezMenu.draw:addParam("myRange", "Draw My Range", SCRIPT_PARAM_ONOFF, true)
	ezMenu.draw:addParam("qDraw", "Draw Q Range", SCRIPT_PARAM_ONOFF, true)
	ezMenu.draw:addParam("wDraw", "Draw W Range", SCRIPT_PARAM_ONOFF, true)
	ezMenu.draw:addParam("eDraw", "Draw E Range", SCRIPT_PARAM_ONOFF, true)
	ezMenu.draw:addParam("sliderQ","Draw VPred Q Range",SCRIPT_PARAM_ONOFF, true)
	ezMenu.draw:addParam("sliderW","Draw VPred W Range",SCRIPT_PARAM_ONOFF, true)
	ezMenu.draw:addParam("sliderR","Draw R usage",SCRIPT_PARAM_ONOFF, true)
--Target Selector
	ts = TargetSelector(TARGET_LESS_CAST_PRIORITY, qRange, true)
	ts.name = "Ezreal MMA"
	ezMenu.key:addTS(ts)
end
function scriptVariables()
	scriptName = myHero.charName
	qReady, wReady, eReady, rReady = false, false, false, false
	qRange, qWidth, qSpeed, qDelay = 1100.0, 80.0, 1200.0, 0.251
	wRange, wWidth, wSpeed, wDelay = 1000.0, 80.0, 1200.0, 0.5
	eRange, eWidth, eSpeed, eDelay = 475.0, 0, 0, 0.5
	rRange, rWidth, rSpeed, rDelay = 2000, 160.0, 2000.0, 1
	myRange = myHero.range
--VPrediction
	VP = VPrediction()
	enemyMinions = minionManager(MINION_ENEMY, qRange, myHero, MINION_SORT_HEALTH_ASC)
	qMinCol = Collision(qRange, qSpeed, qDelay, qWidth)
	rCol = Collision(rRange, rSpeed, rDelat, rWidth)
--Farming Damage to take Q into account
	qDmg, aaDmg, extraDmg, totalDmg = 0,0,0,0
--Dmg Calc Variables
end

function OnLoad()
	scriptVariables()
	menu()
	PrintFloatText(myHero,12, scriptName .." MMA Helper Loaded")
end
function OnUnload()
	PrintFloatText(myHero,12, scriptName .." MMA Helper Unloaded")
end
function OnDraw()
	if not ezMenu.draw.allDraw and not myHero.dead then
		if ezMenu.draw.myRange then 
			DrawCircle(myHero.x, myHero.y, myHero.z, myRange, 0x0000ff)
		end
		if qReady and ezMenu.draw.qDraw then 
			DrawCircle(myHero.x, myHero.y, myHero.z, qRange, 0x0000ff)
		end
		if wReady and ezMenu.draw.wDraw then
			DrawCircle(myHero.x, myHero.y, myHero.z, wRange, 0x20B2AA)
		end
		if eReady and ezMenu.draw.eDraw then
			DrawCircle(myHero.x, myHero.y, myHero.z, eRange, 0xff0000)
		end
		if qReady and ezMenu.draw.sliderQ then 
			DrawCircle(myHero.x, myHero.y, myHero.z, ezMenu.vPred.qChange, 0x0000ff)
		end
		if wReady and ezMenu.draw.sliderW then
			DrawCircle(myHero.x, myHero.y, myHero.z, ezMenu.vPred.wChange, 0x20B2AA)
		end
		if rReady and ezMenu.draw.sliderR then 
			DrawCircle(myHero.x, myHero.y, myHero.z, ezMenu.vPred.rChange, 0x0000ff)
		end
	end
end

function OnTick()
	checks()
	combo(Target)
	farm()	
end
function checks()
	qReady = (myHero:CanUseSpell(_Q) == READY)
        wReady = (myHero:CanUseSpell(_W) == READY)
        eReady = (myHero:CanUseSpell(_E) == READY)
        rReady = (myHero:CanUseSpell(_R) == READY)
	Target = GetMMATarget()
	enemyMinions:update()
	
	--Items
	sheenSlot = GetInventorySlotItem(3057)
	sheenReady = (sheenSlot ~= nil and myHero:CanUseSpell(sheenSlot) == READY) 
        trinitySlot = GetInventorySlotItem(3078)
	triReady = (trinitySlot ~= nil and myHero:CanUseSpell(trinitySlot) == READY) 
	lichBaneSlot = GetInventorySlotItem(3100)
	lichBaneReady = (lichBaneSlot ~= nil and myHero:CanUseSpell(lichBaneSlot) == READY) 
	iceBornSlot = GetInventorySlotItem(3025)
	iceBornReady = (iceBornSlot ~= nil and myHero:CanUseSpell(iceBornSlot) == READY) 
		
end
function GetMMATarget()
    if _G.MMA_Target ~= nil and _G.MMA_Target.type == "obj_AI_Hero" then
    	return _G.MMA_Target
    else
    	ts:update()
    	return ts.target
    end
end
--Combo Cast Function
function combo(target)
	if _G.MMA_Orbwalker or _G.MMA_HybridMode then
		castQ(target)
		castW(target)
		castR()
	end
end
--Spell Cast Functions
function castQ(unit)
	local qKey = ezMenu.key.comboQ
	local qRangeSlider = ezMenu.vPred.qChange
	for _, minion in pairs(enemyMinions.objects) do
		if ValidTarget(unit) and qKey then
			CastPosition,  HitChance,  Position = VP:GetLineCastPosition(unit, qDelay, qWidth, qRangeSlider, qSpeed, myHero)
			if qReady and HitChance >= ezMenu.vPred.qHitChance and GetDistance(CastPosition) <= qRangeSlider then 
				local willCollide = qMinCol:GetMinionCollision(myHero,CastPosition)
				if not willCollide then 
					Packet('S_CAST', {spellId = _Q, fromX =  CastPosition.x, fromY = CastPosition.z}):send() 
				end
			end
		end
	end
end
--Will AARest for MMA
function castW(unit)
	local wKey = ezMenu.key.comboW
	local wRangeSlider = ezMenu.vPred.wChange
	if ValidTarget(unit) and wKey then
		CastPosition,  HitChance,  Position = VP:GetLineCastPosition(unit, wDelay, wWidth, wRangeSlider, wSpeed, myHero)
		if wReady and HitChance >= ezMenu.vPred.wHitChance and GetDistance(CastPosition) <= wRangeSlider then 
			Packet('S_CAST', {spellId = _W, fromX =  CastPosition.x, fromY = CastPosition.z}):send()
			_G.MMA_ResetAutoAttack()
		end
	end
end
function castR()
	for _, enemy in pairs(GetEnemyHeroes()) do
		local rKey = ezMenu.key.comboR
		local rDmg = rDmgReductionCalc() 
		if ValidTarget(enemy) and rKey and not enemy.dead then
			CastPosition,  HitChance,  Position = VP:GetLineCastPosition(enemy, rDelay, rWidth, rRange, rSpeed, myHero)
			if rReady and HitChance >= ezMenu.vPred.rHitChance and GetDistance(CastPosition) >= ezMenu.vPred.rChange  and rDmgReductionCalc() > enemy.health then 
				Packet('S_CAST', {spellId = _R, fromX =  CastPosition.x, fromY = CastPosition.z}):send()
			end
		end
	end
end
--Farming
function farm()
local qKey = ezMenu.farm.useQ
	for _, minion in pairs(enemyMinions.objects) do
	local qDmg = getDmg("Q",minion,myHero)
	local aaDmg = getDmg("AD",minion,myHero)
	local extraDmg = (sheenSlot and aaDmg or 0) + (trinitySlot and aaDmg * 1.5 or 0) + (lichBaneSlot and getDmg("LICHBANE",minion,myHero) or 0) + (iceBornSlot and aaDmg*1.25 or 0)
	local totalDmg = aaDmg + extraDmg
	local mana = myHero.mana
	local percMana = ezMenu.farm.farmQMana
	local willCollide = qMinCol:GetMinionCollision(myHero,minion)
		if _G.MMA_LastHit and qKey and mana >= percMana then
			if ValidTarget(minion) then
				if GetDistance(minion) <= qRange and qReady and totalDmg >= minion.health then
					if not willCollide then Packet('S_CAST', {spellId = _Q, fromX =  minion.x, fromY = minion.z}):send()end
				end
			end
		end
	end		
end
function rDmgReductionCalc()
	for _, enemy in pairs(GetEnemyHeroes()) do
		if ValidTarget(enemy) then
			local willCollide = rCol:GetMinionCollision(myHero,enemy)
			local rDmg = getDmg("R",enemy,myHero)
			local minPercentReduction, damageReduction, percentReduction, rTotalDmg = .30, 0, 0, 0
			local minionCount = rCol:GetMinionCount(myHero,enemy)
			if minionCount >= 1 then
				percentReduction = minionCount * 0.10
				if percentReduction <= minPercentReduction then
					damageReduction = rDmg * minPercentReduction
					rTotalDmg = rDmg - percentReduction
				else if percentReduction > minPercentReduction then
					percentReduction = minionCount * 0.10
					damageReduction = rDmg * minPercentReduction
					rTotalDmg = rDmg - percentReduction
				end
				end
			else
				rTotalDmg = rDmg
			end
		end
	end
	return(rTotalDmg)
end
